package day13.Ti.synchronizedDome;

public class Ti02 {
    public static void main(String[] args) {
        MyRunnable2 mr = new MyRunnable2();
        Thread t1 = new Thread(mr,"窗口一");
        Thread t2 = new Thread(mr,"窗口二");
        Thread t3 = new Thread(mr,"窗口三");
        t1.start();
        t2.start();
        t3.start();
    }
}

class MyRunnable2 implements Runnable {
    private int ticket = 100;


    @Override
    public void run() {
        while (true) {

            if (method()) break;
            synchronized (this){
//            synchronized (MyRunnable2.class){

            }
        }
    }

    private synchronized boolean method() {
        if (ticket <= 0) {
            System.out.println("卖完了");
            return true;
        } else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {


            }
            ticket--;
            System.out.println(Thread.currentThread().getName() + "在卖票,剩余" + ticket);

    }
        return false;
    }
}
